<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="dataForm" size="mini" :model="listQuery" label-position="right" label-width="auto">
        <div class="lineBox">
          <el-form-item label="应用模板：" prop="deptName">
            <el-select v-model="listQuery.template_id">
              <el-option label="全部" value="0" />
              <el-option v-for="( item ,index ) in templateList" :key="item.id" :label="item.template_name"
                :value="item.id" />
            </el-select>
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-form-item label="限制处理队列选择上次已匹配模板：" prop="deduilieptName">
            <el-select v-model="listQuery.xianzhi_template_id">
              <el-option label="无" value="0" />
              <el-option v-for="( item ,index ) in templateList" :key="item.id" :label="item.template_name"
                :value="item.id" />
            </el-select>
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-form-item label="没有抽取出的内容" prop="duilie">
            <el-select v-model="listQuery.status">
              <el-option label="是" value="1" />
              <el-option label="否" value="2" />
            </el-select>
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-form-item label="处理条数（不填表示不限）:" prop="deptName">
            <el-input v-model="listQuery.num" size="mini" />
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-button v-if="status==2" size="mini" type="primary" @click="startUp">
            启动
          </el-button>

          <el-button v-if="status==1" size="mini" type="danger" disabled="true">
            运行中
          </el-button>
        </div>
        <div class="lineBox">
          <el-button v-if="status==1" size="mini" style="margin-left: 10px;" type="primary" @click="cancel">
            取消
          </el-button>
        </div>
      </el-form>
    </div>

    <div class="myTable" style="min-height: 600px;">
      <el-table :key="tableKey" :data="list" fit size="mini" highlight-current-row style="width: 100%;">
        <el-table-column label="ID" prop="id" align="center" min-width="50" />
        <el-table-column label="启动时间" prop="start_time" align="center" min-width="200" />
        <el-table-column label="结束时间" prop="end_time" align="center" min-width="100" />
        <el-table-column label="模板" prop="template_name" show-overflow-tooltip align="center" min-width="200">
          <template slot-scope="{row,$index}">
            <span v-if="row.template_name">{{ row.template_name }}</span>
            <span v-if="!row.template_name">全部</span>
          </template>
        </el-table-column>
        <el-table-column label="处理档案数量" prop="files_num" align="center" min-width="50" />
        <el-table-column label="分类成功数" prop="files_num_succ" align="center" min-width="50" />
        <el-table-column label="实体关系数" prop="entity_num" align="center" min-width="50" />
        <el-table-column label="属性数" prop="attributes_num" align="center" min-width="50" />
        <el-table-column label="操作" align="center" min-width="160" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="skip(row,$index)">
              查看相关档案
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination size="mini" :page-sizes="[10]" :total="total" :page.sync="listQuery.page"
        :limit.sync="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" @pagination="dataList" />
    </div>
  </div>
</template>

<script>
import { api } from '@/api/template'
import { taskApi } from '@/api/task'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {

  components: { Pagination },

  data() {
    return {
      status: "",
      templateList: [],
      formInline: {
        user: "",
        region: ""
      },
      value: '',
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        duilie: "1",
        pageNumber: 1,
        page: 1,
        limit: 10,
        template_id: 0,
        num: null,
        id: null,
        duilie: null,
        status: null,
        xianzhi_template_id: null,
      },


    }
  },



  async created() {

    await this.getAllTemplate()
    await this.getLastTask()
    await this.dataList()
  },
  methods: {

    async getLastTask() {
      const url = "getLastTask";
      const response = await taskApi(url, this.listQuery)
      if (response.code == 200) {

        if (response.data == "") {
          this.status = 2;
        } else {
          this.status = response.data.status
          this.listQuery.num = response.data.num;
          this.listQuery.template_id = response.data.template_id;
          this.listQuery.id = response.data.id
        }
      }
    },

    async cancel() {

      const url = "cancel";
      const response = await taskApi(url, this.listQuery)

      if (response.code == 200) {


        this.status = 2;


        this.$message({
          message: '取消成功',
          type: 'success'
        });
      } else {
        this.$message({
          message: '取消失败',
          type: 'error'
        });
      }
    },

    async startUp() {

      const url = "startUp";
      const response = await taskApi(url, this.listQuery)

      if (response.code == 200) {


        this.status = response.data.status
        this.listQuery.id = response.data.id


        this.$message({
          message: '启动成功',
          type: 'success'
        });
      } else {
        this.$message({
          message: '启动失败',
          type: 'error'
        });
      }


    },

    // 数据列表
    async dataList() {
      this.listLoading = true
      const response = await taskApi("getTaskApiList", this.listQuery)
      if (response.code == 200) {
        this.list = response.data.resultList
        this.total = response.totalCount
        // this.type = response.type
        //   // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      }
    },


    async getAllTemplate() {

      const url = 'getAllTemplate'
      var res = await api(url, {})
      if (res.code == 200) {
        this.templateList = res.data;
      }
    },


    // 修改跳转
    skip(row, index) {

      this.$router.push('/template/archivesPage?id=' + row.id)

    },


  }
}
</script>

<style lang="scss" scoped>
.demo-image__preview {
  float: left;
  margin-left: 5px;
}
.demonstration {
  font-size: 12px;
  color: #333;
  font-weight: normal;
}
</style>
